//
//  IfengCycleScrollView.m
//  IfengNews
//
//  Created by 王 芬 on 14-7-2.
//
//

#import "IfengCycleScrollView.h"

@implementation IfengCycleScrollView

@synthesize scrollView = _scrollView;
@synthesize currentPage = _curPage;
@synthesize datasource = _datasource;
@synthesize delegate = _delegate;

- (void)dealloc
{
  
}

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    // Initialization code
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(self.bounds.size.width * 3, self.bounds.size.height);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.scrollsToTop=NO;
      _scrollView.bounces=NO;
    _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    _scrollView.pagingEnabled = YES;
    [self addSubview:_scrollView];
    
    _curPage = 0;
  }
  return self;
}

- (void)setDataource:(id<CycleScrollViewDatasource>)datasource
{
  _datasource = datasource;
}

- (void)reloadData
{
  _totalPages = [_datasource cSGetNumberOfPages];
  if (_totalPages == 0) {
    return;
  }
  _scrollView.scrollEnabled = (1==_totalPages)?NO:YES;
  _curPage = 0;
  [self loadData];
  [self.delegate cSLoadDataAtIndex:_curPage];
}

- (void)loadData
{
  //从scrollView上移除所有的subview
  NSArray *subViews = [_scrollView subviews];
  if([subViews count] != 0) {
    [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  }
  
  [self getDisplayImagesWithCurpage:_curPage];
  
  for (int i = 0; i < 3; i++) {
    UIView *v = [_curViews objectAtIndex:i];
    CGRect frame = v.frame;
    frame.origin.x = i*frame.size.width;
    v.frame = frame;
    [_scrollView addSubview:v];
  }
  if (1==_totalPages)
    [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width*2, 0)];
  else
  [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0)];
}

- (void)getDisplayImagesWithCurpage:(int)page {
  
  int pre = [self validPageValue:_curPage-1];
  int last = [self validPageValue:_curPage+1];
  
  if (!_curViews) {
    _curViews = [[NSMutableArray alloc] init];
  }
  
  [_curViews removeAllObjects];

    [_curViews addObject:[_datasource cSGetPageViewAtIndex:pre]];
    [_curViews addObject:[_datasource cSGetPageViewAtIndex:page]];
    [_curViews addObject:[_datasource cSGetPageViewAtIndex:last]];
}

- (int)validPageValue:(NSInteger)value {
  
  if(value == -1) value = _totalPages - 1;
  if(value == _totalPages) value = 0;
  
  return value;
  
}

- (void)setViewContent:(UIView *)view atIndex:(NSInteger)index
{
  if (index == _curPage) {
    [_curViews replaceObjectAtIndex:1 withObject:view];
    for (int i = 0; i < 3; i++) {
      UIView *v = [_curViews objectAtIndex:i];
      v.frame = CGRectOffset(v.frame, v.frame.size.width * i, 0);
      [_scrollView addSubview:v];
    }
  }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
  int x = aScrollView.contentOffset.x;
  
  //往下翻一张
  if(x >= (2*self.frame.size.width)) {
    _curPage = [self validPageValue:_curPage+1];
    [self loadData];
  }
  
  //往上翻
  if(x <= 0) {
    _curPage = [self validPageValue:_curPage-1];
    [self loadData];
  }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
 //[self.delegate cSLoadDataAtIndex:_curPage];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView {
  
  [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:YES];
  [self.delegate cSLoadDataAtIndex:_curPage];

}

@end
